package com.thoughtworks.collection;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int sum=0;
        if(leftBorder>rightBorder){
            for(int i=rightBorder;i<=leftBorder;i++){
                if(i%2==0){
                    sum=sum+i;
                }
            }
        }else{
            for(int i=leftBorder;i<=rightBorder;i++){
                if(i%2==0){
                    sum=sum+i;
                }
            }
        }
      return sum;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int sum=0;
        if(leftBorder>rightBorder){
            for(int i=rightBorder;i<=leftBorder;i++){
                if(i%2!=0){
                    sum=sum+i;
                }
            }
        }else{
            for(int i=leftBorder;i<=rightBorder;i++){
                if(i%2!=0){
                    sum=sum+i;
                }
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int sum=0;
        for(int i=0;i<integers.length;i++){
            int i1 = integers[i] * 3 + 2;
            sum=sum+i1;
        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2!=0){
                int i1 = integers[i] * 3 + 2;
                integers[i]=i1;
            }
        }
        List<Integer> integers1 = Arrays.asList(integers);
        return integers1;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int sum=0;
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2!=0){
                int i1 = integers[i] * 3 + 5;
                sum=sum+i1;
            }
        }
        return sum;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int sum=0;
        int ou=0;
        int zhongwei=0;
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2==0){
                sum=sum+integers[i];
                ou++;
                zhongwei =sum/ou;
            }
        }

        return zhongwei;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int sum=0;
        int ou=0;
        int zhongwei=0;
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2==0){
                sum=sum+integers[i];
                ou++;
                zhongwei =sum/ou;
            }
        }
        return zhongwei;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        Integer [] result=new Integer[integers.length];
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2!=0){
               result[i]=integers[i];
            }
        }
        for(int i=0;i<result.length;i++){
            if(result[i].equals(specialElment)){
                return true;
            }
            return false;
        }
        return true;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        Stream<Integer> integerStream = arrayList.stream().filter(a -> a % 2 == 0);
        return integerStream.collect(Collectors.toList());
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        Stream<Integer> sorted = arrayList.stream().filter(a -> a % 2 == 0).sorted(Integer::compareTo);
        Stream<Integer> sorted1 = arrayList.stream().filter(a -> a % 2 != 0).sorted(Comparator.reverseOrder());
        Stream<Integer> concat = Stream.concat(sorted, sorted1);
        return concat.collect(Collectors.toList());
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        for(int i=0;i<integers.length;i++){
                int i1 = (integers[i]+integers[i+1])*3;
                integers[i]=i1;
        }
        List<Integer> integers1 = Arrays.asList(integers);
        return integers1;
    }
}
